<?php

class StoreModel extends BpfModel
{

  public function createItemCate($form)
  {
    return ItemCate::create($form);
  }

  public function getItemCate($cateid, $type = 'items_cates.cate_id')
  {
    static $list = [];
    $cacheid = $cateid . $type;
    if (empty($list[$cacheid])) {
      $mysqlModel = $this->getModel('mysql');
      $itemCate = $mysqlModel->getSqlBuilder()
        ->from('items_cates')
        ->where($type, $cateid)
        ->query()->row('ItemCate');
      $list[$cacheid] = $itemCate;
    }
    return $list[$cacheid];
  }

  private function _setitemCatesFilter($query, $filter)
  {
    $query->from('items_cates')
      ->join('users', 'items_cates.user_id = users.user_id');
    if (isset($filter)) {
      if (!empty($filter['search'])) {
        $search = '%' . $filter['search'] . '%';
        $query->WhereGroupStart();
        $query->Where('users.username LIKE', $search);
        $query->orWhere('items_cates.name LIKE', $search);
        $query->whereGroupEnd();
      }
      if (isset($filter['status']) && is_numeric($filter['status'])) {
        $query->Where('items_cates.status', $filter['status']);
      }
    }
  }

  public function getItemCatesCount($filter = null)
  {
    $mysqlModel = $this->getModel('mysql');
    $query = $mysqlModel->getSqlBuilder()
        ->select('COUNT(0)');
    $this->_setitemCatesFilter($query, $filter);
    return $query->query()->field();
  }

  public function getItemCatesList($filter = null, $page = 1, $rows = 10)
  {
    $mysqlModel = $this->getModel('mysql');
    $query = $mysqlModel->getSqlBuilder()
        ->select('items_cates.*, users.nickname')
        ->limitPage($rows, $page);
    $this->_setitemCatesFilter($query, $filter);
    if (isset($filter['created ASC']) ) {
      $query->orderby('items_cates.created asc');
    } else {
      $query->orderby('items_cates.created desc');
    }
    return $query->query()->all('ItemCate');
  }

  public function createItem($from)
  {
    return Item::create($from);
  }

  public function getItem($itemid, $type = 'items.item_id')
  {
    static $list = [];
    $cacheid = $itemid . $type;
    if (empty($list[$cacheid])) {
      $mysqlModel = $this->getModel('mysql');
      $item = $mysqlModel->getSqlBuilder()
        ->from('items')
        ->where($type, $itemid)
        ->query()->row('Item');
      $list[$cacheid] = $item;
    }
    return $list[$cacheid];
  }

  private function _setItemsFilter($query, $filter)
  {
    $query->from('items')
      ->join('users', 'items.user_id = users.user_id')
      ->join('items_cates', 'items.cate_id = items_cates.cate_id');
    if (isset($filter)) {
      if (!empty($filter['search'])) {
        $search = '%' . $filter['search'] . '%';
        $query->WhereGroupStart();
        $query->Where('users.username LIKE', $search);
        $query->orWhere('items.name LIKE', $search);
        $query->whereGroupEnd();
      }
      if (isset($filter['status']) && is_numeric($filter['status'])) {
        $query->Where('items.status', $filter['status']);
      }
      if (isset($filter['cate_id']) && is_numeric($filter['cate_id'])) {
        $query->Where('items.cate_id', $filter['cate_id']);
      }
    }
  }

  public function getItemsCount($filter = null)
  {
    $mysqlModel = $this->getModel('mysql');
    $query = $mysqlModel->getSqlBuilder()
      ->select('COUNT(0)');
    $this->_setItemsFilter($query, $filter);
    return $query->query()->field();
  }

  public function getItemsList($filter = null, $page = 1, $rows = 10)
  {
    $mysqlModel = $this->getModel('mysql');
    $query = $mysqlModel->getSqlBuilder()
        ->select('items.*, users.nickname')
        ->select('items_cates.name AS cate')

        ->limitPage($rows, $page);
    $this->_setItemsFilter($query, $filter);
    if (isset($filter['created ASC']) ) {
      $query->orderby('items.created asc');
    } else {
      $query->orderby('items.created desc');
    }
    return $query->query()->all('Item');
  }

}


class ItemCate extends BpfObject
{
  const IS_OFFLINE = 0;
  const IS_ONLINE = 1;

  public static function create($form)
  {
    $cateid = BpfObject::getModel('mysql')->getSqlBuilder()->insert('items_cates', [
      'user_id' => intval($form['user_id']),
      'name' => trim($form['name']),
      'status' => ItemCate::IS_OFFLINE,
      'created' => REQUEST_TIME,
      'updated' => REQUEST_TIME
    ])->query()->insertId();
    return BpfObject::getModel('store')->getItemCate($cateid);
  }

  public function getId()
  {
    return $this->cate_id;
  }

  public function delete()
  {
    //检查该类目下还有没有商品
    if ($this->getModel('store')->getItemsCount(['cate_id' => $this->cate_id])) {
      return false;
    }
    return $this->getModel('mysql')->getSqlBuilder()->delete('items_cates', [
      'cate_id' => $this->cate_id
    ])->query()->affectedRows();
  }


  public function update()
  {
    return $this->getModel('mysql')->getSqlBuilder()->update('items_cates', [
      'status' => intval($this->status),
      'updated' => REQUEST_TIME
    ], ['cate_id' => $this->cate_id])->query()->affectedRows();
  }

}

class Item extends BpfObject
{
  const IS_OFFLINE = 0;
  const IS_ONLINE = 1;

  public static function create($form)
  {
    $lifetime = intval('0' . $form['lifetime']);
    $flowers = intval('0' . $form['flowers']);
    $itemid = BpfObject::getModel('mysql')->getSqlBuilder()->insert('items', [
      'user_id' => intval($form['user_id']),
      'cate_id' => intval($form['cate_id']),
      'image' => trim($form['image']),
      'name' => trim($form['name']),
      'describe' => trim($form['describe']),
      'lifetime' => $lifetime, 'flowers' => $flowers,
      'income' => round($flowers/$lifetime, 9),
      'status' => intval($form['status']),
      'created' => REQUEST_TIME,
      'updated' => REQUEST_TIME
    ])->query()->insertId();
    return BpfObject::getModel('store')->getItem($itemid);
  }

  public function getId()
  {
    return $this->item_id;
  }

  public function delete()
  {
    //删除正在上架的商品
    $this->getModel('mysql')->getSqlBuilder()->delete('stores_items', [
      'item_id' => $this->item_id
    ])->query()->affectedRows();
    //删除商品
    return $this->getModel('mysql')->getSqlBuilder()->delete('items', [
      'item_id' => $this->item_id
    ])->query()->affectedRows();
  }

  public function update()
  {
    return $this->getModel('mysql')->getSqlBuilder()->update('items', [
      'status' => intval($this->status),
      'updated' => REQUEST_TIME
    ], ['item_id' => $this->item_id])->query()->affectedRows();
  }

}
